<?php

namespace app\web\controller;

use AlibabaCloud\Dbs\Dbs;
use app\common\controller\Api;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;


use app\common\service\FeiEPrintService;
use app\common\service\OrdinaryService;
use app\common\service\PrintService;
use app\common\service\ZipFileService;
use Carbon\Carbon;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use my\Pinyin;
use plugins\FeiE\FeiEPrintClass;
use app\common\service\FuYouPayService;
use think\Db;
use think\facade\Env;

/**
 * 员工控制器
 * @package app\index\controller
 */
class Test extends Auth
{
    public $noNeedLogin = ['*'];

    /**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return Dysmsapi Client
     */
    public static function createClient($accessKeyId, $accessKeySecret)
    {
        $config = new Config([
            // 您的AccessKey ID
            "accessKeyId" => $accessKeyId,
            // 您的AccessKey Secret
            "accessKeySecret" => $accessKeySecret
        ]);
        // 访问的域名
        $config->endpoint = "dysmsapi.aliyuncs.com";
        return new Dysmsapi($config);
    }

    /**
     * @param string[] $args
     * @return void
     */
    public static function main()
    {
        $client = self::createClient("LTAI2UzMicjqIxKt", "OPeXmeSSDsA5dIddsQchvItYoT8V9x");
        $sendSmsRequest = new SendSmsRequest([
            "phoneNumbers" => "18039920816",
            "signName" => "萌小呆",
            "templateCode" => "SMS_204750968",
            "templateParam" => "{\"code\":\"123456\"}"
        ]);
        // 复制代码运行请自行打印 API 的返回值
        $client->sendSms($sendSmsRequest);
        $client->sendBatchSmsWithOptions($sendSmsRequest);
    }

    /**
     * 打印手机结账账单
     */
    public static function index()
    {
       $array[0] =  [
            "number" => 10,
            "status" => 5,
            "project_id" => 19,
            "project_name" =>"传统足浴",
            "is_add" => 1,
       ];
        $array[1] =  [
            "number" => 11,
            "status" => 2,
            "project_id" => 20,
            "project_name" =>"精油SPA",
            "is_add" => 1,
        ];
        $commission = \app\common\model\EngineerConfig::calculateCommission($array);
        $item['commission'] += $commission['commission'];
        $item['add_commission']  += $commission['add_commission'];


//        $project = Db::name('qt_project')->where('id', '40')->find();
//        $commission = new OrdinaryService();
//        $commission = $commission->showCommission('2', '1', $project);
        dump(json_encode($array));die;
    }


    //生成订单号
    private static function create_ordersn()
    {
        $yCode = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N');
        $orderSn = $yCode[intval(date('Y')) - 2019] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
        return $orderSn;
    }


    public function getImage()
    {
        //将要下载的图片数组
        $image[] = array(
            'image_src' => "http://houtai1.kamiyunhui.com/uploads/qrcode/1664380800-1.png",
            'image_name' => "1664380800-1.png"
        );
        //下面是实例操作过程：
        $dfile = tempnam('/tmp', 'tmp');//产生一个临时文件，用于缓存下载文件
        $zip = new ZipFileService();
        //----------------------
        $filename = 'image.zip'; //下载的默认文件名

        //以下是需要下载的图片数组信息，将需要下载的图片信息转化为类似即可
        foreach ($image as $v) {
            $zip->add_file(file_get_contents($v['image_src']),
                iconv('utf-8', 'GB18030', $v['image_name']));
            // 添加打包的图片，第一个参数是图片内容，第二个参数是压缩包里面的显示的名称,
            //   可包含路径
            // 或是想打包整个目录 用 $zip->add_path($image_path);
        }
        //----------------------
        $zip->output($dfile);

        // 下载文件
        ob_clean();
        header('Pragma: public');
        header('Last-Modified:' . gmdate('D, d M Y H:i:s') . 'GMT');
        header('Cache-Control:no-store, no-cache, must-revalidate');
        header('Cache-Control:pre-check=0, post-check=0, max-age=0');
        header('Content-Transfer-Encoding:binary');
        header('Content-Encoding:none');
        header('Content-type:multipart/form-data');
        //设置下载的默认文件名
        header('Content-Disposition:attachment; filename="' . $filename . '"');
        header('Content-length:' . filesize($dfile));
        $fp = fopen($dfile, 'r');
        while (connection_status() == 0 && $buf = @fread($fp, 8192)) {
            echo $buf;
        }
        fclose($fp);
        @unlink($dfile);
        @flush();
        @ob_flush();
        exit;
    }


    public static function test($arr, $A, $B, $C, $D, $E, $F)
    {
        $orderInfo = '<CB>飞鹅云测试</CB><BR>';
        $orderInfo .= '名称      单价      数量    金额   姓名   优惠<BR>';
        $orderInfo .= '------------------------------------------------<BR>';
        foreach ($arr as $k5 => $v5) {
            $name = $v5['title'];
            $price = $v5['price'];
            $num = $v5['num'];
            $prices = $v5['price'] * $v5['num'];
            $user = $v5['name'];
            $dis = $v5['dis'];

            $kw3 = '';
            $kw1 = '';
            $kw2 = '';
            $kw4 = '';
            $kw5 = '';
            $kw6 = '';
            $str = $name;
            $blankNum = $A;//名称控制为14个字节
            $lan = mb_strlen($str, 'utf-8');
            $m = 0;
            $j = 1;
            $blankNum++;
            $result = array();
            if (strlen($price) < $B) {
                $k1 = $B - strlen($price);
                for ($q = 0; $q < $k1; $q++) {
                    $kw1 .= ' ';
                }
                $price = $kw1 . $price;
            }
            if (strlen($num) < $C) {
                $k2 = $C - strlen($num);
                for ($q = 0; $q < $k2; $q++) {
                    $kw2 .= ' ';
                }
                $num = $kw2 . $num;
            }
            if (strlen($prices) < $D) {
                $k3 = $D - strlen($prices);
                for ($q = 0; $q < $k3; $q++) {
                    $kw3 .= ' ';
                }
                $prices = $kw3 . $prices;
            }

            if (strlen($user) < $E) {
                $k4 = $E - strlen($user);
                for ($q = 0; $q < $k4; $q++) {
                    $kw4 .= ' ';
                }
                $user = $kw4 . $user;
            }
            if (strlen($dis) < $F) {
                $k5 = $F - strlen($dis);
                for ($q = 0; $q < $k5; $q++) {
                    $kw6 .= ' ';
                }
                $dis = $kw6 . $dis;
            }

            for ($i = 0; $i < $lan; $i++) {
                $new = mb_substr($str, $m, $j, 'utf-8');
                $j++;
                if (mb_strwidth($new, 'utf-8') < $blankNum) {
                    if ($m + $j > $lan) {
                        $m = $m + $j;
                        $tail = $new;
                        $lenght = iconv("UTF-8", "GBK//IGNORE", $new);
                        $k = $A - strlen($lenght);
                        for ($q = 0; $q < $k; $q++) {
                            $kw5 .= ' ';
                        }
                        if ($m == $j) {
                            $tail .= $kw5 . ' ' . $price . ' ' . $num . ' ' . $prices . ' ' . $user . ' ' . $dis;
                        } else {
                            $tail .= $kw5 . '<BR>';
                        }
                        break;
                    } else {
                        $next_new = mb_substr($str, $m, $j, 'utf-8');
                        if (mb_strwidth($next_new, 'utf-8') < $blankNum) continue;
                        else {
                            $m = $i + 1;
                            $result[] = $new;
                            $j = 1;
                        }
                    }
                }
            }
            $head = '';
            foreach ($result as $key => $value) {
                if ($key < 1) {
                    $v_lenght = iconv("UTF-8", "GBK//IGNORE", $value);
                    $v_lenght = strlen($v_lenght);
                    if ($v_lenght == 13) $value = $value . " ";
                    $head .= $value . ' ' . $price . ' ' . $num . ' ' . $prices . ' ' . $user . ' ' . $dis;
                } else {
                    $head .= $value . '<BR>';
                }
            }
            $orderInfo .= $head . $tail;
            @$nums += $prices;
        }
        $time = date('Y-m-d H:i:s', time());
        $orderInfo .= '------------------------------------------------<BR>';
        $orderInfo .= '合计：' . number_format($nums, 1) . '元<BR>';
        $orderInfo .= '送货地点：广州市南沙区xx路xx号<BR>';
        $orderInfo .= '联系电话：020-39004606<BR>';
        $orderInfo .= '订餐时间：' . $time . '<BR>';
        $orderInfo .= '备注：加辣<BR><BR>';
        $orderInfo .= '<QR>http://www.feieyun.com</QR>';//把解析后的二维码生成的字符串用标签套上即可自动生成二维码
        return $orderInfo;
    }


}
